package com.sjgtw.cloud.web.jointDefenceUnit.controller;

import com.sjgtw.cloud.common.core.web.controller.BaseController;
import com.sjgtw.cloud.common.core.web.domain.AjaxResult;
import com.sjgtw.cloud.common.core.web.page.TableDataInfo;
import com.sjgtw.cloud.util.EasyExcelUtil;
import com.sjgtw.cloud.web.drainageRecord.domain.DrainageRecord;
import com.sjgtw.cloud.web.jointDefenceUnit.domain.JointDefenceUnit;
import com.sjgtw.cloud.web.jointDefenceUnit.service.IJointDefenceUnitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 联防单位Controller
 * 
 * @author ruoyi
 * @date 2024-03-09
 */
@RestController
@Api(tags = "1.5、联防单位管理")
@RequestMapping("jointDefenceUnit")
public class JointDefenceUnitController extends BaseController
{
    @Autowired
    private IJointDefenceUnitService jointDefenceUnitService;

    /**
     * 查询联防单位列表
     */
   // @RequiresPermissions("system:联防单位:list")
    @GetMapping("/list")
    @ApiOperation("列表")
    public TableDataInfo list(JointDefenceUnit jointDefenceUnit)
    {
        startPage();
        List<JointDefenceUnit> list = jointDefenceUnitService.selectJointDefenceUnitList(jointDefenceUnit);
        return getDataTable(list);
    }

    /**
     * 导出联防单位列表
     */
   // @RequiresPermissions("system:联防单位:export")
/*
    @PostMapping("/export")
    public void export(HttpServletResponse response, JointDefenceUnit jointDefenceUnit)
    {
        List<JointDefenceUnit> list = jointDefenceUnitService.selectJointDefenceUnitList(jointDefenceUnit);
        ExcelUtil<JointDefenceUnit> util = new ExcelUtil<JointDefenceUnit>(JointDefenceUnit.class);
        try {
            util.exportExcel(response, list, "联防单位数据");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
*/

    /**
     * 获取联防单位详细信息
     */
    //@RequiresPermissions("system:联防单位:query")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取联防单位详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(jointDefenceUnitService.selectJointDefenceUnitById(id));
    }

    /**
     * 新增联防单位
     */
    //@RequiresPermissions("system:联防单位:add")
    @PostMapping
    @ApiOperation("新增")
    public AjaxResult add(@RequestBody JointDefenceUnit jointDefenceUnit)
    {
        return toAjax(jointDefenceUnitService.insertJointDefenceUnit(jointDefenceUnit));
    }

    /**
     * 修改联防单位
     */
    //@RequiresPermissions("system:联防单位:edit")

    @PutMapping
    public AjaxResult edit(@RequestBody JointDefenceUnit jointDefenceUnit)
    {
        return toAjax(jointDefenceUnitService.updateJointDefenceUnit(jointDefenceUnit));
    }

    /**
     * 删除联防单位
     */
    //@RequiresPermissions("system:联防单位:remove")

	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(jointDefenceUnitService.deleteJointDefenceUnitById(id));
    }

    /**
     * 导出
     */
    @ApiOperation(value = "导出")
    @PostMapping("/export")
    public void export(HttpServletResponse response, JointDefenceUnit jointDefenceUnit)
    {
        List<JointDefenceUnit> list = jointDefenceUnitService.selectJointDefenceUnitList(jointDefenceUnit);
        EasyExcelUtil<JointDefenceUnit> util = new EasyExcelUtil<>(JointDefenceUnit.class);
        util.exportExcel(response, list, "联防单位管理", "联防单位管理", "jointDefenceUnit.xlsx");
    }

    /**
     * 下载模版
     */
    @ApiOperation(value = "下载模版")
    @PostMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response)
    {
        EasyExcelUtil<DrainageRecord> util = new EasyExcelUtil<>(DrainageRecord.class);
        util.exportExcel(response, new ArrayList<>(), "联防单位管理", "联防单位管理模版", "jointDefenceUnit.xlsx");
    }

    /**
     * 导入
     */
    @ApiOperation(value = "导入")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        EasyExcelUtil<JointDefenceUnit> util = new EasyExcelUtil<>(JointDefenceUnit.class);
        List<JointDefenceUnit> entities = util.readExcel(file, 0, 1);
        if (entities == null || entities.size() == 0) {
            return error("没有读取到数据");
        }
        List<String> errors = jointDefenceUnitService.check(entities);
        if (errors.size() > 0) {
            return AjaxResult.error("", errors);
        }

        jointDefenceUnitService.batchSave(entities);
        return success();
    }

}
